import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class node{
    int x;
    int y;
    char n;
    node(int x,int y,char n){
        this.x=x;
        this.y=y;
        this.n=n;
    }
}
public class demo02 {
    //长草
    public static int[] dx={1,0,0,-1};
    public static int[] dy={0,1,-1,0};
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m=sc.nextInt();
        char[][] arr=new char[n][m];
        String[] arr1=new String[n];
        for (int i = 0; i < n; i++) {
            arr1[i]=sc.next();
            arr[i]=arr1[i].toCharArray();
        }
        int k=sc.nextInt();
        Queue<node> queue=new LinkedList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(arr[i][j]=='g'){
                    node tmp=new node(i,j,'g');
                    queue.offer(tmp);
                }
            }
        }
        while (!queue.isEmpty()&&k>0){
            int size=queue.size();
            while (size>0) {
                node tmp1 = queue.poll();
                for (int i = 0; i < 4; i++) {
                    int i1 = tmp1.x + dx[i];
                    int j1 = tmp1.y + dy[i];
                    if (i1 >= 0 && i1 < n && j1 >= 0 && j1 < m) {
                        if (arr[i1][j1] == '.') {
                            arr[i1][j1] = 'g';
                            node tmp2 = new node(i1, j1, 'g');
                            queue.offer(tmp2);
                        }
                    }
                }
                size--;
            }
            k--;
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
}
